//
//  PEFirmwareDetailVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/3.
//

#import "RHLocalFirmwareDetailVC.h"
#import "RHLocalFirmwareUpgradeVC.h"

@interface RHLocalFirmwareDetailVC ()
//固件标题
@property (nonatomic ,strong) QMUILabel *titleLab;
//固件版本
@property (nonatomic ,strong) QMUILabel *versionLab;
//文件大小
@property (nonatomic ,strong) QMUILabel *sizeLab;
//固件描述
@property (nonatomic ,strong) QMUILabel *describeLab;
@end

@implementation RHLocalFirmwareDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"固件详情".rh_localized;
    [self setupUI];
}

- (void)setupUI{
    kWeakSelf;
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = CFTabViewBgColor;
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    UIView *contentView = kView;
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.layer.cornerRadius = 8;
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 15, 15, 15));
        make.width.offset(kUI_WINDOW_W-30);
        make.height.greaterThanOrEqualTo(@(kUI_WINDOW_H-kNavHeight - 30));
    }];
    
    UIImageView *firmwareIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ugrade_firmware"]];
    [contentView addSubview:firmwareIcon];
 
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setTextColor:kCOLOR_HEX(@"323233")];
    [_titleLab setFont:[UIFont boldSystemFontOfSize:15]];
    [contentView addSubview:_titleLab];
    
    _versionLab = [[QMUILabel alloc] init];
    [_versionLab setTextAlignment:NSTextAlignmentLeft];
    [_versionLab setTextColor:kCOLOR_HEX(@"323233")];
    [_versionLab setFont:[UIFont systemFontOfSize:12]];
    [contentView addSubview:_versionLab];
    
    _sizeLab = [[QMUILabel alloc] init];
    [_sizeLab setTextAlignment:NSTextAlignmentLeft];
    [_sizeLab setTextColor:kCOLOR_HEX(@"323233")];
    [_sizeLab setFont:[UIFont systemFontOfSize:12]];
    [contentView addSubview:_sizeLab];
    
    
    UIView *desView = kView;
    desView.layer.cornerRadius = 8;
    desView.backgroundColor = CFTabViewBgColor;
    [contentView addSubview:desView];
    
    _describeLab = [[QMUILabel alloc] init];
    [_describeLab setTextAlignment:NSTextAlignmentLeft];
    [_describeLab setTextColor:[kCOLOR_HEX(@"323233") colorWithAlphaComponent:0.6]];
    [_describeLab setFont:[UIFont systemFontOfSize:12]];
    [_describeLab setNumberOfLines:1];
    [desView addSubview:_describeLab];
    
    QMUILabel *tipsLab = [[QMUILabel alloc] init];
    [tipsLab setTextAlignment:NSTextAlignmentLeft];
    [tipsLab setTextColor:kColorRGBA(63, 68, 67, 0.6)];
    [tipsLab setFont:[UIFont systemFontOfSize:12]];
    [tipsLab setNumberOfLines:0];
    tipsLab.text = @"提示：升级前请保持APP和设备蓝牙链接正常，手机和设备电量充足。整个升级过程大概需要2分钟，期间请勿操作和使用设备，否则可能有安全风险。".rh_localized;
    tipsLab.qmui_lineHeight = 20;
    [contentView addSubview:tipsLab];
    
    
    QMUIButton *upgradeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [upgradeBtn setTitle:@"升级".rh_localized forState:UIControlStateNormal];
    [upgradeBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    upgradeBtn.titleLabel.font = kSYSTEMFONT(16);
    [upgradeBtn setGradientBackgroundWithColors:@[kRGB(106, 215, 133),kRGB(94, 203, 183)] orientation:GradientOrientationLeftToRight];
    [upgradeBtn setCornerRadius:8];
    [contentView addSubview:upgradeBtn];

  
    
    [firmwareIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(firmwareIcon.mas_right).offset(10);
        make.top.equalTo(firmwareIcon).offset(8);
        make.height.mas_equalTo(15);
        make.right.mas_offset(-15);
    }];
    
    [_versionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab);
        make.bottom.equalTo(firmwareIcon.mas_bottom).offset(-8);
        make.height.mas_equalTo(20);
    }];
    
    [_sizeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.versionLab.mas_right).offset(10);
        make.centerY.equalTo(weakSelf.versionLab);
        make.right.mas_offset(-5).priorityLow();
    }];
    
    [desView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(firmwareIcon.mas_bottom).offset(35);
        make.right.mas_offset(-15);
    }];
    
    [_describeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 15, 15, 15));
    }];
    
    [tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(-15);
    }];
    
    [upgradeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(50);
        make.bottom.equalTo(tipsLab.mas_top).offset(-10);
    }];
    
    self.titleLab.text = self.firmwareModel.endpoint;
    self.describeLab.text = self.firmwareModel.endpointDescribe;
    self.versionLab.text = [NSString stringWithFormat:@"%@:%@",@"版本".rh_localized,ValidNullDataToString(self.firmwareModel.firmwareNumber, @"--")];
    
    NSString *sizeStr = [NSString stringWithFormat:@"%@:--",@"固件大小".rh_localized];
    if([self.firmwareModel.size integerValue]>1000){
        CGFloat size = [self.firmwareModel.size integerValue]/1024.0;
        sizeStr = [NSString stringWithFormat:@"%@:%@MB",@"固件大小".rh_localized,[@(size) twoDecimalPlacesWithoutRounding]];
    }else if ([self.firmwareModel.size integerValue]>0){
        
        sizeStr = [NSString stringWithFormat:@"%@:%ldKB",@"固件大小".rh_localized,[self.firmwareModel.size integerValue]];
    }
    
    self.sizeLab.text = sizeStr;
    
    
    [[upgradeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        RHLocalFirmwareUpgradeVC *upgradeVC = [[RHLocalFirmwareUpgradeVC alloc]init];
        upgradeVC.firmwareModel = weakSelf.firmwareModel;
        [weakSelf.navigationController pushViewController:upgradeVC animated:YES];
    }];
}

@end
